{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c32dc279",
   "metadata": {},
   "source": [
    "1、列表的基础用法\n",
    "2、列表的高级用法（在列表中使用切片）\n",
    "3、购物车程序\n",
    "## 知识回顾（变量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0fbea623",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字对象\n",
    "import random\n",
    "wait_time = random.randint(1,60)  # 将随机数赋给waittime变量\n",
    "wait_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3cb54dcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jinshanlee will be myth'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串对象\n",
    "word = \"jinshanlee will be myth\"\n",
    "word"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51b5fbfc",
   "metadata": {},
   "source": [
    "python中一切都是变量，所有对象都可以赋予变量\n",
    "## 初步学习4个内置数据结构\n",
    "列表、元组、字典、集合\n",
    "- 有序集合可变/不可变\n",
    "- 列表：有序的可变对象集合（列表中的每个元素从0开始编号）\n",
    "- 列表是可变的，可以在任何时间通过增加、删除或者修改对象\n",
    "- 列表就像一组数组 他存的对象顺序放置在槽中\n",
    "- 元组：有序的不可变对象集合\n",
    "- 元组是一个不可变的列表，一旦向一个元组赋对象，任何情况下这个元组不能再改变\n",
    "- 元组可以确保你的对象不被你的（或者其他人）代码修改\n",
    "---\n",
    "## 无序数据结构\n",
    "- 字典：无序的键/值对集合（字典是无序且可变的一种数据结构）\n",
    "- 在字典中每个唯一键有一个与之关联的值。字典可以包含多个值对或者键"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa38e6d",
   "metadata": {},
   "source": [
    "- 与键关联的值可以是任意的对象\n",
    "- 集合：无序的唯一对象集合（一种避免重复的数据结构，集合不允许有重复的对象）\n",
    "- 集合可以用来保存相关对象的一个集合，同时确保其中任何对象不会重复\n",
    "- 与列表和字典类似，集合可以根据需要扩展（和收缩）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2da5da70",
   "metadata": {},
   "source": [
    "# 一、列表的基础用法\n",
    "111、创建字面列表（直接将对象赋给一个新列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2e173c9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = []\n",
    "temps = [23.0,21.2,32.1,12.5]\n",
    "words = ['jinshan','jiajiajia']\n",
    "phone_details = ['iphone','ios15.1',5799,128.1]\n",
    "odds_and_ends = [1,2,3],['a','b','c']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "91dfef3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jin\n"
     ]
    }
   ],
   "source": [
    "# 使用列表\n",
    "# 先定一个一个名字为vowels的列表\n",
    "vowels = ['lee','jin','shan']\n",
    "# 需要检查的单词，创建一个变量\n",
    "word = \"jin\",\"tian\"\n",
    "# 询问一个对象在另一个对象中吗 用in来检查\n",
    "for item in word:\n",
    "    if item in vowels:\n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9e38f00",
   "metadata": {},
   "source": [
    "# 在运行时扩展列表\n",
    "- append（）圆括号的内容是（比较大的一个范围，python中一切皆是对象）\n",
    "- append新增在末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bc67ce83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = []\n",
    "len(vowels) # len 内置函数会报告一个对象的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d65a6a08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jinshan']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 确认vowels列表中没有任何对象后 使用append增加新的列表\n",
    "vowels.append('jinshan')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "54b17dfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7574ddbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jinshan', 'o']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断对象在不在vowels里面\n",
    "if 'o' not in vowels:\n",
    "    vowels.append('o')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "94ad4c55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 完善代码\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = \"millways\"\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51dbd758",
   "metadata": {},
   "source": [
    "# 列表的高级用法\n",
    "- 赋值操作符\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b0d9f67b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = [1,2,3,4,5,6]\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ad0858b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second = first\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2eb4c0b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 8]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second.append(8)\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d88d0044",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 8]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a46c6e3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 8]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 实现真正的复制 copy用法\n",
    "third = second.copy()\n",
    "third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "970fb05c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 8]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second # 使用copy方法复制，新对象添加到third中\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4973bedf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u', ' ', '!']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 2.3中括号的扩展\n",
    "#正索引值从左向右数，负索引值从右向左数\n",
    "phrase = \"i love you !\"\n",
    "line = list(phrase)\n",
    "line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "82ee8530",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "line[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5f8fb799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "line[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15e9cc34",
   "metadata": {},
   "source": [
    "# 列表中的开始，结束和步长值\n",
    "- start值允许你控制范围从哪里开始\n",
    "- 用于列表时，开始值指示开始索引值\n",
    "- stop值允许你控制范围何时结束\n",
    "- 用于列表时，结束值指示到哪个索引值结束，但不包括这个索引值\n",
    "- step值允许你控制范围如何生成\n",
    "- 用于列表时，步长值指示每一步大小\n",
    "# 用于列表时，可以把开始、结束和步长值放在中括号里，相互用冒号字符分隔\n",
    "- letters（start、stop、step）可以指定开始结束和步长值\n",
    "- 如果没有指定开始值，则默认值为0\n",
    "- 如果没有指定结束值，则取列表允许的最大值\n",
    "- 如果没有指定步长值，则默认1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c6af6055",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'e', 'l', 'e', 'b', 'r', 'i', 't', 'y', '1', '7', '3']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sentence = \"celebrity173\"\n",
    "letter = list(sentence)\n",
    "letter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "97694113",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['e', 'b', 'r', 'i', 't', 'y', '1', '7', '3']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 切片的用法\n",
    "letter[3:]# 跳过前三个字母，然后给出其他字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6d69535b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'e', 'l', 'e', 'b', 'r', 'i', 't', 'y', '1']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[:10]  #直到索引位置10的所有字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c9880789",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'e', 'i', '1']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[0:10:3]   # 每3个字母选1个，直到索引位置10（不包括10）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1a07fe8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'l', 'b', 'i', 'y', '7']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letter[::2]  # 每两个字母选择一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6aaac9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
